#pragma comment(lib, "glfwdll.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "SOIL.lib")
//#pragma comment(lib, "user32.lib")
//#define WIN32_LEAN_AND_MEAN
//#include <windows.h>

#include <gl\glfw.h>
#include <gl\gl.h>
#include <SOIL.h>

#include <stdio.h>
#include <cstring>
#include <string>

char image_to_load[256];


using std::string;

//
void loadTexture()
{
	/* load an image file directly as a new OpenGL texture */
	GLuint tex_2d = SOIL_load_OGL_texture
		(
			image_to_load,
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS

		);
		
	/* check for an error during the load process */
	if( 0 == tex_2d )
	{
		printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
	}

}

void quadCentered(float x,float y,float siz)
{
glBegin(GL_QUADS);

glTexCoord2f(0,0); glVertex2f(x-siz,y-siz);
glTexCoord2f(1,0); glVertex2f(x+siz,y-siz);
glTexCoord2f(1,1); glVertex2f(x+siz,y+siz);
glTexCoord2f(0,1); glVertex2f(x-siz,y+siz);

glEnd();
}


int main(int argc, char *argv[])
{
	for(int i=0; i<argc; i++)
	{
		printf("%s\n", argv[i] );
	}
	
	if( argc > 1 )
	{
		strcpy(image_to_load, argv[1] );
	}
	
	glfwInit();

	int xres = 640;
	int yres = 480;
	glfwOpenWindow(xres,yres,0,0,0,0,0,0,GLFW_WINDOW);
	
	string strExtensions = "";
	const GLubyte *exts = glGetString(GL_EXTENSIONS);
	int extCount = 0;
	for(int i=0; i<strlen( (char*)exts); i++)
	{
		char c = exts[i];
		if( c == ' ')
		{
			printf("%\n");
			strExtensions += "\n";
			extCount++;
		}else
		{
			printf("%c",c);
			strExtensions += c;
		}
	}

	char title[200];
	sprintf_s(title,200, "%d opengl exts", extCount);

	const GLubyte *strVendor = glGetString(GL_VENDOR);
	const GLubyte *strRender = glGetString(GL_RENDER);
	const GLubyte *strVersion = glGetString(GL_VERSION);
	printf("GL_VENDOR:%s\n", strVendor);
	printf("GL_RENDER:%s\n", strRender);
	printf("GL_VERSION:%s\n", strVersion);
	
	int majorVer;
	int minorVer;
	int revision;
	glfwGetGLVersion(&majorVer, &minorVer, &revision);
	printf("GL Version %i.%i rev %i\n", majorVer, minorVer, revision);

	//MessageBox(0x0, strExtensions.c_str() , title , MB_OK);

	loadTexture();
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0,xres,yres,0,-1,1); // Left Right Bottom Top Near Far
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	glClearColor(1,1,1,1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	
	//glDisable(GL_CULL_FACE);
	//glDisable(GL_DEPTH_TEST);
	glColor3f(1,1,1);
	glEnable(GL_TEXTURE_2D);
	quadCentered(xres/2, yres/2, 300);

	glfwSwapBuffers();

	glfwSleep(1);
	glfwCloseWindow();

	glfwTerminate();
}